package mulMan.gui.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;

import mulMan.domain.PlaylistModel;
import mulMan.gui.view.AddPlaylistView;
import mulMan.gui.view.ModifyPlaylistView;
import mulMan.gui.view.PlaylistView;

public class PlaylistController extends Observable{
	
	private PlaylistModel playlist;
	private PlaylistView view;
	
	
	public PlaylistController(PlaylistModel playlist, PlaylistView view) {
		this.playlist = playlist;
		this.view = view;
		
		if (view instanceof AddPlaylistView) {
			((AddPlaylistView)view).addAddPlaylistListener(new AddPlaylistListener());
		} else if (view instanceof ModifyPlaylistView) {
			((ModifyPlaylistView)view).addModifyPlaylistListener(new ModifyPlaylistListener());
		}
	}
	
	
	//ACTION LISTENERS
	
	class AddPlaylistListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	playlist.setName(view.getNameTextField().getText());
	    	try {
				playlist.savePlaylist();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
	    	((AddPlaylistView)view).getButtonAdd().setEnabled(false);
	    	((AddPlaylistView)view).dispose();
	    }
	}
	
	class ModifyPlaylistListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	playlist.setName(view.getNameTextField().getText());
	    	try {
				playlist.updatePlaylist();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
	    	((ModifyPlaylistView)view).getButtonModify().setEnabled(false);
	    	((ModifyPlaylistView)view).dispose();
	    }
	}
}
